package com.puff.controller.admin;

import com.puff.result.Result;
import com.puff.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {

    private static final String KEY = "SHOP_STATUS_";

    private static final String PERMISSION = "PLAT_STATUS_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ShopService shopService;

    /**
     * 设置店铺的营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺的营业状态")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置店铺的营业状态为：{}",status == 1 ? "营业中" : "打烊中");
//        Integer permission = (Integer) redisTemplate.opsForValue().get(PERMISSION + "1");
//        if (permission == 0){
//            return Result.error("平台端已强制停止营业！");
//        }
        redisTemplate.opsForValue().set(KEY+"1",status);
        // 同步平台端商家状态
        shopService.startOrStopShop(1L,status);
        return Result.success();
    }

    /**
     * 获取店铺的营业状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("获取店铺的营业状态")
    public Result<Integer> getStatus(){
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY+"1");
        log.info("获取店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
        return Result.success(status);
    }
}
